<template>
<view>
	<u-loading-page :loading="loadstatus"></u-loading-page>
	<view v-if="playad==1">
		
	</view>
	<view class="u-padding-30">
		<u--text type="primary" :text="name" size="30" align="center" class="u-m-b-10"></u--text>
		<u-empty :text="emptytxt" class="u-m-t-80" mode="data"  icon="http://ws.yodin.com/storage/empty/data.png" :show="taginfo.cate.length === 0" v-if="loadstatus==false"></u-empty>
		
		<view v-if="taginfo.cate.length>0">
			<view v-for="(cate, index) in taginfo.cate" :key="index">
				<u-divider :text="cate.categoryname" textSize="24" textColor="#d4237a" lineColor="#13227a" :hairline="false"></u-divider>
				<boxList :druglist="cate" :listcount="cate.listcount"  :tagname="taginfo.tagname" :categoryid="cate.categoryid"></boxList>
			</view>
		</view>
		
	</view>
	<u-gap height="70"></u-gap>
</view>
</template>


<script>
import boxList from "@/components/box-list.vue";
import {mapState,mapMutations} from 'vuex';
import base from "@/config/baseUrl";
export default {
	components: {
	  boxList
	},
	data(){
		return {
			loadstatus:false,
			taginfo:{
				tagname:'',
				cate:[]
			},
			playad:0,
			name:'',
			emptytxt:'加载中'
		};
	},
	computed: {
	  ...mapState(['user']),
	},
	onShareAppMessage: function (res) {
		if (res.from === 'button') {}
		if (this.hasLogin){
			let data = {s:this.user.userInfo.salt}
		}else{
			let data = {s:''}
		}
		return {
			title: '邻邻社',
			path: '?query='+encodeURIComponent(JSON.stringify(data)),
			success: function(res) {},
			fail: function(res) {}
		}
	},

	onPullDownRefresh() {
			this.taginfo = [];
			this.getTags();
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 100);
	},

	
	async onLoad(options) {
		let query = JSON.parse(decodeURIComponent(options.query));
		this.name=query.name
		this.playad=query.playad
		if (this.playad==1){
		  // #ifdef MP
		  this.adload()
		  // #endif
		}
		this.taginfo =await this.getTags()
	},

	methods:{
		gift(adid){
			return new Promise((resolve, reject) => {
					this.$api('gift', {
					name:this.name,
					adid:adid
				}).then(res => {
					if (res.code==200){
						this.$tools.msg(res.msg);
					}
				}).catch((e)=>{
					resolve(false);
				});
			})
		},
		adload(){
			
	// 在页面中定义激励视频广告
	let videoAd = null
	let that =this
	// 在页面onLoad回调事件中创建激励视频广告实例
	if (wx.createRewardedVideoAd) {
	  videoAd = wx.createRewardedVideoAd({
		adUnitId: 'adunit-3667d25656309a66'
	  })
	  videoAd.onLoad(() => {})
	  videoAd.onError((err) => {})
	  //videoAd.onClose((res) => {})
      // 监听关闭
      videoAd.onClose((status) => {
        if (status && status.isEnded || status === undefined) {
          // 正常播放结束，下发奖励
          that.gift('3667d25656309a66')
        } else {
			this.$tools.msg("广告未播放完毕，不会有签到积分哦");
        }
	   })
	  
	  
	  
	  
	}

	// 用户触发广告后，显示激励视频广告
	if (videoAd) {
	  videoAd.show().catch(() => {
		// 失败重试
		videoAd.load()
		  .then(() => videoAd.show())
		  .catch(err => {
			console.log('激励视频 广告显示失败')
		  })
	  })
	}
			
			
		},
		getTags(){
			return new Promise((resolve , reject)=>{
				this.loadstatus=true;
					this.$api('dcd.search',{
							name:this.name
						}).then(res => {
							console.log('getTags',res)
							if (res.code==200){
								this.taginfo=res.data
								if (this.taginfo.cate.length>0){
									
								}else{
									this.emptytxt="抱歉，没有找到相关信息"
								}
								this.loadstatus=false
								//resolve(res.data)
							}
					}).catch(e => {this.loadstatus=false})
			})
		},
	}
	
};
</script>
<style lang="scss" scoped>

</style>
